<template>
  <div class="table">
    <div class="crumbs">
      <i class="el-icon-tickets">评论信息</i>
    </div>
    <div class="container">
      <div class="handle-box">
        <el-button type="primary" size="mini" @click="delAll">批量删除</el-button>
        <el-input v-model="select_word" size="mini" placeholder="筛选关键词" class="handle-input"/>
      </div>
    </div>
    <el-table size="mini" border style="width: 100%" :data="tableData" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="40px"></el-table-column>

      <el-table-column prop="name" label="用户名"  align="center"/>
      <el-table-column prop="content" label="评论内容"  align="center"/>

      <el-table-column label="操作" width="150px" align="center">
        <template slot-scope="scope">
          <el-button size="mini" type="danger" @click="handleDelete(scope.row.id)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog title="删除评论" :visible.sync="delVisible" width="300px" center>
      <div align="center">
        删除不可恢复，是否确定删除
      </div>
      <span slot="footer">
        <el-button size="mini" @click="deleteRow">
          确定
        </el-button>
        <el-button size="mini" @click="delVisible = false">
          取消
        </el-button>
    </span>
    </el-dialog>
  </div>
</template>

<script>
import {mixin} from '../mixins/index'
import {
  searchSongId, getCollectOfUserId, delCollect,
  getAllCommentBySongListId, delComment, getUserOfId, searchSingerId, loginTest
} from '../api/index'

export default {
  mixins: [mixin],
  name: 'Comment',
  props: ['id'],
  data () {
    return {
      delVisible: false,
      registerForm: {
        singerName: '',
        songName: ''
      },
      tableData: [],
      tempData: [],
      select_word: '',
      // 当前选择项
      idx: '',
      // 哪些项已经打勾
      multipleSelection: [],
      songListId: ''
    }
  },
  watch: {
    select_word: function() {
      if (this.select_word === '') {
        this.tableData = this.tempData
      } else {
        this.tableData = []
        for (let item of this.tempData) {
          if (item.name.includes(this.select_word)) {
            this.tableData.push(item)
          }
        }
      }
    }
  },
  created () {
    this.songListId = this.$route.query.id
    this.getData()
  },
  methods: {
    getData() {
      this.tempData = []
      this.tableData = []
      getAllCommentBySongListId(this.songListId).then(res => {
        for (let item of res.data) {
          this.getUsers(item.userId, item)
        }
      })
    },
    getUsers(id, item) {
      getUserOfId(id).then(res => {
        let o = item
        o.name = res.data.username
        this.tempData.push(o)
        this.tableData.push(o)
      }).catch(err => {
        console.log(err)
      })
    },
    deleteRow() {
      delComment(this.idx).then(res => {
        if (res.code === 200) {
          this.getData()
          this.$message.success('删除成功')
        } else {
          this.$message.error('删除失败')
        }
      })
      this.delVisible = false
    }
  }
}
</script>

<style scoped>
  .handle-box {
    margin-bottom: 20px;
  }
  .song-img{
    width: 100%;
    height: 80px;
    border-radius: 5px;
    margin-bottom: 5px;
    overflow: hidden;
  }
  .handle-input{
    width: 300px;
    display: inline-block;
  }
  .pagination{
    display: flex;
    justify-content: center;
  }
  .play{
    position: absolute;
    z-index: 100;
    width: 80px;
    height: 80px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    top: 18px;
    left: 15px;
  }
  .icon{
    width: 2em;
    height: 2em;
    color: white;
    fill: currentColor;
    overflow: hidden;
  }
</style>
